
export function randomNum(n1,n2) {
    var res = parseInt(Math.random() * (n2 - n1 + 1) + n1) ;
    return res;
}

export function formatTime(date) {
    var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;

      var d = date.getDate();
      d = d < 10 ? "0" + d : d;

      var h = date.getHours();
      h = h < 10 ? "0" + h : h;

      var min = date.getMinutes();
      min = min < 10 ? "0" + min : min;

      var s = date.getSeconds();
      s = s < 10 ? "0" + s : s;

      var day = date.getDay();
      // 0-6
      var week = [
        "星期天",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ];

      var res = `${y}-${m}-${d} ${h}:${min}:${s} ${week[day]}`;
     
    return res;
}

export function formatTime1(date,type) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? "0" + m : m;

    var d = date.getDate();
    d = d < 10 ? "0" + d : d;
    
    var res = type ? `${y}${type}${m}${type}${d}`:`${y}年${m}月${d}日`;
     
    return res;
}

export function hmsTime(date) {
    var h = date.getHours();
    h = h < 10 ? "0" + h : h;

    var min = date.getMinutes();
    min = min < 10 ? "0" + min : min;

    var s = date.getSeconds();
    s = s < 10 ? "0" + s : s;

    var res = `${h}:${min}:${s}`
    
    return res;
}


export function differenceTime(date1,date2) {
    var time1 = date1.getTime();
    var time2 = date2.getTime();

    var time = Math.abs(time1 - time2);

    var day = parseInt(time / 1000 / 60 / 60 / 24);

    var hours = parseInt((time / 1000 / 60 / 60) % 24);

    var min = parseInt((time / 1000 / 60) % 60);

    var sec = parseInt((time / 1000) % 60);
    var obj = {
        day: day,
        hours: hours,
        min: min,
        sec:sec
    }
    return obj;
}

export function getStyle(ele, attr) {

    var style;
    if (window.getComputedStyle) {
        style = window.getComputedStyle(ele)[attr];
    } else {
        style = ele.currentStyle[attr];
    }

    return style;
}

export function addEvent(ele,type,callback) {
    if (window.addEventListener) {
      ele.addEventListener(type,callback)   
    } else {
        ele.attachEvent('on'+type,callback)
    }
}


export function animation1(ele, attr, target) {
    let move = () => {
        let l = parseInt(getStyle(ele, attr));
        
        // l = 9 target = 0
        // (0-9)/10 = -0.9  向上取整为0
        // -0.9 ===》-1
        let speed = (target - l) / 10;
        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
        
        l += speed;
        timer = requestAnimationFrame(move);

        if (target == l) {
            cancelAnimationFrame(timer);
        }
        ele.style[attr] = l + "px";
    };
    var timer = setTimeout(move, 17);
}


export function animation(ele, option, callback) {
    // 用于记录动画的个数
    let len = 0;
    //遍历这个对象 对象有几个属性 开启几个动画
    for (const attr in option) {
        len++;
        cancelAnimationFrame(ele[attr])

        ele[attr] = requestAnimationFrame(move);
        function move() {
            let l = parseInt(getStyle(ele, attr));
            let speed = (option[attr] - l) / 30;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            
            l += speed;
            ele[attr] = requestAnimationFrame(move);

            if (option[attr] == l) {
                cancelAnimationFrame(ele[attr]);
                // 清除一个动画 len-1
                len--;
                // 动画执行结束的地方
                // 把回调函数写在这 有多好个属性参与动画 ，这个回调函数就会执行多少次
                // 动画真正结束：各个属性的动画都结束之后 这叫做动画执行（一次）
            }
            ele.style[attr] = l + "px";
            // 当len = 0的时候 表示所有的动画都结束
            if (len == 0) {
                callback && callback();
            }
        };
    } 
}